package com.gw.booking.controller.common;


import com.gw.booking.common.*;
import com.gw.booking.dto.backend.CityListDtoOut;
import com.gw.booking.dto.backend.MerchantTypeListDtoOut;
import com.gw.booking.service.CityService;
import com.gw.booking.service.LoginTokenService;
import com.gw.booking.service.MerchantTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Api(tags = {"共通-共通接口"})
@Controller
@RequestMapping("/commons")
public class CCommonController {



    @Autowired
    private LoginTokenService loginTokenService;
    @Autowired
    private CityService cityService;
    @Autowired
    private MerchantTypeService merchantTypeService;

    @ApiOperation(value = "检测登录状态")
    @RequestMapping(value = "checkLoginState.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject checkLoginState(HttpServletRequest request) {
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "注销")
    @RequestMapping(value = "logout.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject logout(HttpServletRequest request) {
        loginTokenService.txDelete(Util.getLoginToken(request));

        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "城市列表")
    @RequestMapping(value = "cityList.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject<List<CityListDtoOut>> cityList(HttpServletRequest request) {
        return new ResultObject(MessageCode.CODE_SUCCESS, cityService.list());
    }

    @ApiOperation(value = "商家分类列表")
    @RequestMapping(value = "/list.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<List<MerchantTypeListDtoOut>> list(HttpServletRequest request)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, merchantTypeService.list());
    }

//    @ApiOperation(value = "测试")
//    @RequestMapping(value = "test.do", method = {RequestMethod.POST})
//    @ResponseBody
//    public ResultObject test(HttpServletRequest request) throws Exception {
//        File file = new File(request.getServletContext().getRealPath("")+logoUrl);
//        System.out.println(file.exists());
//        System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
//        return new ResultObject(MessageCode.CODE_SUCCESS, file.length());
//    }
}
